package Articulo;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nan
 */
public class MedGModificar implements ActionListener, KeyListener{

    private GModifica Ingreso = new GModifica();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private String clave = "";
    private Articulo.articulosListar articulos;

    public MedGModificar(){

        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.setLocationRelativeTo(null);
        Ingreso.show();
    }

    MedGModificar(articulosListar articulo) {
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        this.articulos = articulo;
        Ingreso.setLocationRelativeTo(null);
        Ingreso.show();

    }

    public void actionPerformed(ActionEvent e) {
        
        if(e.getSource() == getIngreso().getjButtonModificar()){
            String a = (getIngreso().getjTextFieldNombre().getText());
            String b =(getIngreso().getjTextFieldmarca().getText());
            String c =(getIngreso().getjTextFielddescripcion().getText());
            float d = Float.parseFloat(getIngreso().getjTextFieldprecioCosto().getText());
            String ee =(getIngreso().getjTextFieldStockMin().getText());
            String f =(getIngreso().getjTextFieldStockMax().getText());
            String g =(getIngreso().getStock().getText());
            conectar.conectar();
            Statement st = null;
            try {
                st = conectar.getConn().createStatement();
                String modifica = "UPDATE \"Articulo\" SET nombre='"+a+"', marca='"+b+"', descripcion='"+c+"', \"precioCosto\"="+d+", \"StockMin\"="+ee+", \"StockMax\"="+f+", \"Stock\"="+g+" WHERE \"codArt\" = '"+getClave()+"';";
                st.execute(modifica);
                st.close();
                vaciarCampos();
                this.articulos.actualizarTabla();
                Ingreso.dispose();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Nose puede insertar", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
            conectar.cerrar();
        }
        if(e.getSource() == getIngreso().getjButtonCancelar()){
            getIngreso().dispose();
        }
    }

    public void keyTyped(KeyEvent e) {
        if (e.getSource()== getIngreso().getjTextFieldStockMin()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getjTextFieldStockMax()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getjTextFieldprecioCosto()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) && (caracter != '.')){
                e.consume();
            }
            if (caracter == '.' && getIngreso().getjTextFieldprecioCosto().getText().contains(".")){
                e.consume();
            }
        }
        if (e.getSource()== getIngreso().getStock()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
    }

    public void keyPressed(KeyEvent e) {

        if (e.getSource() == Ingreso.getjTextFieldNombre() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldmarca().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFieldmarca() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFielddescripcion().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFielddescripcion() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldprecioCosto().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFieldprecioCosto() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldStockMin().requestFocus();
        } if (e.getSource() == Ingreso.getjTextFieldStockMin() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldStockMax().requestFocus();
        } if (e.getSource() == Ingreso.getjTextFieldStockMax() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getStock().requestFocus();
        }if (e.getSource() == Ingreso.getStock() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjButtonAceptar().doClick();
        }
    }

    public void keyReleased(KeyEvent e) {   }

    public void vaciarCampos(){
        getIngreso().getjTextFieldNombre().setText("");
        getIngreso().getjTextFieldmarca().setText("");
        getIngreso().getjTextFielddescripcion().setText("");
        getIngreso().getjTextFieldprecioCosto().setText("");
        getIngreso().getjTextFieldStockMin().setText("");
        getIngreso().getjTextFieldStockMax().setText("");
        getIngreso().getStock().setText("");
        setClave("");
    }

    /**
     * @return the Ingreso
     */
    public GModifica getIngreso() {
        return Ingreso;
    }

    /**
     * @return the clave
     */
    public String getClave() {
        return clave;
    }

    /**
     * @param clave the clave to set
     */
    public void setClave(String clave) {
        this.clave = clave;
    }

    public void setVisible(boolean mostrar){
        Ingreso.setVisible(mostrar);
    }

   
}
